package com.cx.caigou.baojia.service.impl;

import com.cx.caigou.baojia.mapper.BaoJiaMapper;
import com.cx.caigou.baojia.model.*;
import com.cx.caigou.baojia.service.BaoJiaService;
import com.cx.caigou.config.ResultVO;
import com.cx.caigou.config.ResultVOBuilder;
import com.cx.caigou.request.model.RequestClass;
import com.cx.caigou.request.model.SpareClass;
import com.cx.caigou.util.*;
import io.swagger.models.auth.In;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Random;

@Service
public class Impl implements BaoJiaService {

    @Autowired
    private BaoJiaMapper baoJiaMapper;

    @Override
    public ResultVO query(BaoJiaSelect baoJiaSelect) {
        List<BaoJiaClass> requestList = baoJiaMapper.queryData(baoJiaSelect);

       /* for (BaoJiaClass baoJiaClass:requestList) {
            baoJiaClass.setMOQ(baoJiaClass.getMOQName().toString());
        }*/
        int count =  baoJiaMapper.queryCount(baoJiaSelect);

        AllrecordsVo allrecordsVo = new AllrecordsVo();
        allrecordsVo.setPageIndex(baoJiaSelect.getPageIndex());
        allrecordsVo.setPageSize(baoJiaSelect.getPageSize());
        allrecordsVo.setTotalNumber(count);
        allrecordsVo.setDataList(requestList);
        allrecordsVo.resettotalNum(count);

        return ResultVOBuilder.success(allrecordsVo);

    }

    @Override
    public ResultVO queryByLegalPerson(Integer legalPersonId) {
        List<SupplierClass> gysPersons =  baoJiaMapper.queryByLegalPerson(legalPersonId);
        return ResultVOBuilder.success(gysPersons);
    }

    @Override
    public ResultVO add(BaoJiaAdd baoJiaAdd) {


        while (true){
            Random random = new Random();
            int ri = random.nextInt(999999999);
            int i = baoJiaMapper.queryIsFind(ri);
            if(i==0){
                baoJiaAdd.setQuotationNumber(ri);
                break;
            }
        }
        //添加报价明细表
        baoJiaMapper.addQuotation(baoJiaAdd);
        //根据单号删除所有的报价单 然后再添加
        baoJiaMapper.deleteDetails(baoJiaAdd.getQuotationNumber());

        baoJiaMapper.addDetaeils(baoJiaAdd.getQuotationNumber(),baoJiaAdd.getQuotationItem());

        return ResultVOBuilder.success();
    }

    @Override
    public ResultVO search(SearvhVo searvhVo) {
        List<QuptationReturnVo> requestList = baoJiaMapper.querySearchData(searvhVo);

        for (QuptationReturnVo s:requestList) {
            s.setCurrencyCodeWord(CurrencyCodeEnum.getStatusName(s.getCurrencyCode()));
            s.setBiddingStatusName(StatusEnum.getStatusName(s.getBiddingStatus()));
        }



        int count =  baoJiaMapper.querySearchCount(searvhVo);

        AllrecordsVo allrecordsVo = new AllrecordsVo();
        allrecordsVo.setPageIndex(searvhVo.getPageIndex());
        allrecordsVo.setPageSize(searvhVo.getPageSize());
        allrecordsVo.setTotalNumber(count);
        allrecordsVo.setDataList(requestList);
        return ResultVOBuilder.success(allrecordsVo);
    }

    @Override
    public ResultVO see(Integer quotationNumber) {
        List<BaoJiaSee> chakan  = baoJiaMapper.see(quotationNumber);
        List<SeeVo> bj = baoJiaMapper.see1(quotationNumber);
        for (BaoJiaSee v: chakan) {
            v.setQuotationItem(bj);
            v.setCurrencyCodeWord(CurrencyCodeEnum.getStatusName(v.getCurrencyCode()));
            v.setIsTaxName(TaxEnum.getStatusName(v.getIsTax()));
            for (SeeVo see: bj ) {
                see.setCurrencyCodeWord(CurrencyCodeEnum.getStatusName(v.getCurrencyCode()));
            }
        }


        return  ResultVOBuilder.success(chakan);
    }

    @Override
    public ResultVO importFile(MultipartFile file) throws Exception{
        ResultVO resultVo = poiImport(file);
        if (!resultVo.isSuccess()){
            return resultVo;
        }
        List<SeeVo> data =(List<SeeVo>)resultVo.getData();
        return ResultVOBuilder.success(data);

    }



    //poi读取信息
    public ResultVO poiImport(MultipartFile file)throws Exception{
        //        3.模板是否是需要导入的模板，验证模板是否正确
        XSSFWorkbook workbook = new XSSFWorkbook(file.getInputStream());
        //获取单元格中的信息 at0是获取sheet1中的数据。
        XSSFSheet sheet = workbook.getSheetAt(0);
        System.out.println(sheet.getRow(sheet.getFirstRowNum()).getCell(0));
        //验证第一列第一行的表格标头信息是否为 “询价单导入”，如果不是，提示模板错误。
        if (!String.valueOf(sheet.getRow(sheet.getFirstRowNum()).getCell(0)).equals("待报价明细导入")) {
            return ResultVOBuilder.error("模板错误，请检查模板！");
        }
        //定义一个集合用来存放表格中的数据
        List<SeeVo> planClassVoList = new ArrayList<>();
        //遍历excel表格中的所有数据，从第五行开始读取，没有数据时终止遍历。
        for (int i = sheet.getFirstRowNum() + 4; i <= sheet.getLastRowNum(); i++) {
            //读取每一行的数据
            XSSFRow xssfRow = sheet.getRow(i);
            if(xssfRow!=null) {
                String test1 = String.valueOf(xssfRow.getCell(1)).trim();
                String test2 = String.valueOf(xssfRow.getCell(2)).trim();
                String test3 = String.valueOf(xssfRow.getCell(3)).trim();
                String test4 = String.valueOf(xssfRow.getCell(4)).trim();
                String test5 = String.valueOf(xssfRow.getCell(5)).trim();
                String test6 = String.valueOf(xssfRow.getCell(6)).trim();
                String test7 = String.valueOf(xssfRow.getCell(7)).trim();
                String test8 = String.valueOf(xssfRow.getCell(8)).trim();
                String test9 = String.valueOf(xssfRow.getCell(9)).trim();
                if (!test1.equals("")&&!test1.equals("null")
                        ||!test2.equals("")&&!test2.equals("null")
                        ||!test3.equals("")&&!test3.equals("null")
                        ||!test4.equals("")&&!test4.equals("null")
                        ||!test5.equals("")&&!test5.equals("null")
                        ||!test6.equals("")&&!test6.equals("null")
                        ||!test7.equals("")&&!test7.equals("null")
                        ||!test8.equals("")&&!test8.equals("null")
                        ||!test9.equals("")&&!test9.equals("null")


                ){
                    //把每一行的数据放入到实体类中
                    ResultVO<SeeVo> inquiryImportVo = build(xssfRow);
                    if (!inquiryImportVo.isSuccess()){
                        return inquiryImportVo;
                    }
                    //把实体类中的数据放入到list集合中
                    planClassVoList.add(inquiryImportVo.getData());
                }

            }
        }
        return ResultVOBuilder.success(planClassVoList);
    }

    private ResultVO<SeeVo> build(XSSFRow xssfRow) {
        SeeVo seeVo = new SeeVo();
        //        4.格式问题，必须保持统一格式，统一为字符串格式导入
        seeVo.setSpareCoding(String.valueOf(xssfRow.getCell(1)));
        seeVo.setSpareName(String.valueOf(xssfRow.getCell(2)));
        seeVo.setMeasurementUnit(String.valueOf(xssfRow.getCell(3)));
        seeVo.setInquiryQty(Integer.parseInt(String.valueOf(xssfRow.getCell(4))));
        seeVo.setQuotationAmount(new BigDecimal(String.valueOf(xssfRow.getCell(5))));
        seeVo.setTaxRate(new BigDecimal(String.valueOf(xssfRow.getCell(6))));
        seeVo.setQuantityAvailable(Integer.parseInt(String.valueOf(xssfRow.getCell(7))));
        seeVo.setMoq(Integer.parseInt(String.valueOf(xssfRow.getCell(8))));
        seeVo.setDeliveryCycle(Integer.parseInt(String.valueOf(xssfRow.getCell(9))));
        return ResultVOBuilder.success(seeVo);
    }


}
